// components/cn-phone/index.js
const nxItem = require('../../behaviors/form/nx-item');
Component({

  /**
   * 组件的属性列表
   */
  behaviors: [nxItem],
  properties: {
    // 可设置为任意原生类型, 如 text number idcard digit
    type: {
      type: String
    },
    disabled: {
      type: Boolean,
      value: false
    },
    isLabelType: {
      type: Boolean,
      value: false
    },
    maxlength: {
      type: Number,
      value: 300
    },
    name: {
      type: String,
      value: ''
    },
    isDefaultPhone: {
      type: Boolean,
      value: false
    },
    isSendCode: {
      type: Boolean,
      value: false
    },
    name: {
      type: String,
      value: ''
    },
    phone: {
      type: Object,
      value: {}
    },
    isShowGetBtn: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    getPhoneNumber: function (e) {
      const msg = e.detail.errMsg
      var sessionID = wx.getStorageSync('user').sessionKey,
        encryptedDataStr = e.detail.encryptedData,
        iv = e.detail.iv;
      if (msg == 'getPhoneNumber:ok') {
        this.triggerEvent('getPhoneNumber', {
          sessionID,
          encryptedDataStr,
          iv,
          msg
        })
      } else if (msg == 'getPhoneNumber:fail user deny') {
        this.triggerEvent('getPhoneNumber', {
          msg,
          isDefaultPhone: false,
          isSendCode: false,
          needCode: true
        })
      } else {
        this.triggerEvent('getPhoneNumber', {
          msg,
          isDefaultPhone: false,
          isSendCode: true,
          needCode: true
        })
      }
    },
    changePhone: function (e) {
      this.triggerEvent('changePhone', {
        phone: e.detail,
        needCode: true,
        codeRequired: true
      })
    },
  },

})